package com.business.system.action;

import java.io.*;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import sun.misc.BASE64Encoder;

import com.business.system.util.FileUtil;

/**
 * @author: xudy
 * @date: 2018/04/15 22:30
 * @description:
 */
@RestController
@RequestMapping("file")
public class FileAction extends BaseAction{
    @RequestMapping("getImg.do")
    public String getImg(String url) {
        String ctxPath = FileUtil.getUploadFilePath();

        url = ctxPath+"/"+url;
        String imgStr = "";
        try {
            File file = new File(url);
            byte[] img_data = null;

            // 读取图片字节数组
            InputStream in = new FileInputStream(file);
            img_data = new byte[in.available()];
            in.read(img_data);
            in.close();
            // 对字节数组Base64编码
            BASE64Encoder encoder = new BASE64Encoder();
            imgStr = encoder.encode(img_data);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return imgStr;
    }
}